package collections.copyonwrite;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 描述：演示数据过期的问题
 */

public class CopyOnWriteArrayListDemo02 {
    public static void main(String[] args) {
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>(new Integer[]{1, 2, 3, 4, 5});
        System.out.println(list);
        Iterator<Integer> itr1 = list.iterator();// itr1看到的数据是1 2 3 4 5
        list.add(6);
        System.out.println(list);
        Iterator<Integer> itr2 = list.iterator();// itr2看到的数据是1 2 3 4 5 6
        itr1.forEachRemaining(System.out::print); // 1 2 3 4 5
        System.out.println();
        itr2.forEachRemaining(System.out::print);// 1 2 3 4 5 6
    }
}

/**
 * output:
 * [a, b, c, d, e] a
 * [a, b, c, d, e] b
 * [a, b, c, d, e] c
 * [a, b, d, e, f] d
 * [a, b, d, e, f] e
 * [a, b, d, e, f]
 */